package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.ObjectUtil;
import com.budwk.app.sqfw.models.Sqfw_rally;
import com.budwk.app.sqfw.models.Sqfw_rally_sign;
import com.budwk.app.sqfw.services.SqfwRallySignForAdminService;
import com.budwk.app.sqfw.vo.SqfwRallyVO;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: wk-mini-dev-env
 * @description: 活动报名（后台管理系统）
 * @author: LJT
 * @create: 2023-11-14 14:21
 **/
@IocBean
@At("/platform/sqfw/sqfwRallySign")
@ApiDefinition(tag = "活动报名（后台管理系统）")
@Slf4j
public class SqfwRallSignForAdminController {

    @Inject
    private SqfwRallySignForAdminService sqfwRallySignForAdminService;


    /**
     * 活动报名列表查询（用于后台管理系统）
     */
    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "活动报名列表查询")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = Sqfw_rally_sign.class
    )
    @ApiResponses(
            implementation = Sqfw_rally_sign.class
    )
    @SLog(tag = "活动报名列表查询", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> list(@Param("..") Sqfw_rally_sign bean, @Param("queryAll") String queryAll, @Param("queryTime") String queryTime, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        // 查询总条数
        List<Sqfw_rally_sign> sqfwRallySignsTotal = sqfwRallySignForAdminService.getRallySignInfoList(bean, queryAll, queryTime, 0, 0);
        // 分页查询
        List<Sqfw_rally_sign> sqfwRallySigns = sqfwRallySignForAdminService.getRallySignInfoList(bean, queryAll, queryTime, pageNo, pageSize);
        Pagination pagination = new Pagination();
        pagination.setList(sqfwRallySigns);
        pagination.setTotalCount(sqfwRallySignsTotal.size());
        return Result.success(pagination);
    }

    /**
     * 根据条件下载Excel
     *
     * @param bean
     * @return
     */
    @At("/generateRallySignExcelByCond")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件下载excel")
    @ApiFormParams(
            implementation = Sqfw_rally_sign.class
    )
    @SLog(tag = "根据条件下载excel", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateRallySignExcelByCond(@Param("..") Sqfw_rally_sign bean, @Param("queryAll") String queryAll, @Param("queryTime") String queryTime) {
        List<Sqfw_rally_sign> sqfwRallySigns = sqfwRallySignForAdminService.getRallySignInfoList(bean, queryAll, queryTime, 0, 0);
        if (sqfwRallySigns.size() <= 0) {
            return Result.error("无匹配记录！");
        }
        List<String> ids = sqfwRallySigns.stream().map(Sqfw_rally_sign::getId).collect(Collectors.toList());
        String userId = SecurityUtil.getUserId();
        String excelUrl = sqfwRallySignForAdminService.createRallyExcelAndUpload(ids);
        if (null == excelUrl) {
            return Result.error("无匹配记录！");
        }
        return Result.data(excelUrl);
    }

    /**
     * 勾选下载Excel
     * @param ids
     */
    @At("/generateRallySignExcelColl")
    @Ok("json")
    @POST
    @ApiOperation(name = "勾选下载Excel")
    @ApiFormParams(
            implementation = Sqfw_rally_sign.class
    )
    @SLog(tag = "勾选下载Excel", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateRallySignExcelColl(@Param("ids") List<String> ids) {
        if (ids.size() <= 0) {
            return Result.error("无匹配记录！");
        }
        Cnd cnd = Cnd.NEW();
        cnd.and("rally_id", "in", ids);
        List<Sqfw_rally_sign> sqfwRallySigns = sqfwRallySignForAdminService.query(cnd);
        if(sqfwRallySigns.size() <= 0) {
            return Result.error("无匹配记录！");
        }
        List<String> sqfwRallSigns = sqfwRallySigns.stream().map(Sqfw_rally_sign::getId).collect(Collectors.toList());
        String excelUrl = sqfwRallySignForAdminService.createRallyExcelAndUpload(sqfwRallSigns);
        if (null == excelUrl) {
            return Result.error("无匹配记录！");
        }
        return Result.data(excelUrl);
    }

    /**
     * 根据上传的type 下载模板
     *
     * @param type
     * @return
     * @Date 2024-01-11
     */
    @At("/getDownloadTemplateFormType")
    @Ok("json")
    @POST
    @ApiOperation(name = "模板下载")
    @SLog(tag = "模板下载", value = "")
    @ApiFormParams({@ApiFormParam(name = "type", description = "类型")})
    @SaCheckLogin
    public Result<?> getDownloadTemplateFromType(@NotNull @Param("type") String type) {
        log.info("getDownloadTemplateFormExcelType info param:type:{}", type);
        return Result.data(sqfwRallySignForAdminService.getDownloadTemplateFormType());
    }

    /**
     * 数量统计
     *
     * @param bean
     * @return
     */
    @At("/count")
    @Ok("json")
    @POST
    @ApiOperation(name = "数量统计")
    @ApiFormParams(
            implementation = Sqfw_rally_sign.class
    )
    @SLog(tag = "数量统计", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> count(@Param("..") Sqfw_rally_sign bean, @Param("queryAll") String queryAll, @Param("queryTime") String queryTime, @Param("ids") String[] ids) {
        List<String> rallyIdTmp = null;
        if (ObjectUtil.isNotEmpty(ids)) {
            rallyIdTmp = new ArrayList<>(Arrays.asList(ids));
        }
        return Result.data(sqfwRallySignForAdminService.count(bean, queryAll, queryTime, rallyIdTmp));
    }

    /**
     * 下载活动总结pdf
     *
     * @param bean
     * @return
     */
    @At("/getRallyReportsPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "下载活动总结pdf")
    @ApiFormParams(
            implementation = SqfwRallyVO.class
    )
    @SLog(tag = "下载活动总结pdf", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getRallyReportsPdf(@Param("..") SqfwRallyVO bean,
                                        @Param("rallyIds") String[] rallyIds) {
        List<String> rallyIdTmp = null;
        if (ObjectUtil.isNotEmpty(rallyIds)) {
            rallyIdTmp = new ArrayList<>(Arrays.asList(rallyIds));
        }
        return Result.data(sqfwRallySignForAdminService.generateRallyReportsPdfColl(bean, rallyIdTmp));
    }

}
